/**
 * 链表结点类
 */
export class ListNode {
    val: number;
    next: ListNode | null;

    constructor (val?: number, next?: ListNode | null) {
        this.val = (val === undefined ? 0 : val);
        this.next = (next === undefined ? null : next);
    }
}

/**
 * 将数组转换为 ListNode 对象
 * @param vals Number 类型数组
 * @returns ListNode 类型对象
 */
export function buildListFromArray (vals: number[]): ListNode | null {
    const vHead = new ListNode();
    let cur = vHead;
    vals.forEach(val => {
        cur.next = new ListNode(val);
        cur = cur.next;
    });
    return vHead.next;
}

/**
 * 将 ListNode 对象 转换为数组
 * @param l ListNode 对象
 * @returns 数字类型数组
 */
export function buildArryFromList (l: ListNode | null): number[] | null {
    if (!l) return null;

    const arr: number[] = [];
    while(l) {
        arr.push(l.val);
        l = l.next;
    }
    return arr;
}